define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置

            var window_innerheight = window.innerHeight;
            var table_height = window_innerheight-75;
            Table.api.init({
                extend: {
                    index_url: 'dingdan/orders/index',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    dragsort_url: '',
                    table: 'dingdan',
                },
                pageSize: Config.pagesize,
                height: table_height
            });

            var table = $("#table");
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#','');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.status = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'user.username', title: "用户名",operate: false,formatter: Controller.api.formatter.setName},
                        {field: 'phone_id', title: "选择云手机",visible: false,addClass: "selectpage", extend: "data-source='dingdan/orders/ajaxPhone' data-field='name' data-page-size='20'",placeholder:"请选择云手机"},

                        {field: 'user_id', title: "选择用户",visible: false,addClass: "selectpage", extend: "data-source='dingdan/orders/ajaxUser' data-field='username' data-page-size='20'",placeholder:"按用户员名称可检索"},
                        {field: 'order_no', title: "订单编号"},

                        {field: 'status', title: "订单状态",formatter: Controller.api.formatter.setStatus,addClass: "selectpage", extend: "data-source='dingdan/orders/ajaxStatus' data-field='name' data-page-size='20'"},


                        {field: 'phone.instance_code', title: "云手机",operate: false},

                        {field: 'nums', title: "时长",operate: false,formatter: Controller.api.formatter.setNums},
                        {field: 'total_price', title: "金额",operate: false},

                        {field: 'type', title: "类型",operate: false,formatter: Controller.api.formatter.setType},
                        {field: 'buy_type', title: "支付方式",operate: false,formatter: Controller.api.formatter.setType2},

                        {field: 'start_time', title: "开始时间",operate: false},
                        {field: 'end_time', title: '结束时间',operate: false},
                        {field: 'remark', title: '备注',operate: false},
                        {field: 'create_time', title: '创建时间',addclass:'datetimerange w200', operate:'RANGE', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);


            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            $(".btn-add").data("area", ["50%", "50%"]);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["50%", "50%"]);
                $(".show_detail").data("area", ["50%", "50%"]);
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        check: function () {
            Controller.api.bindevent();
        },
        diaohuo: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                setType:function (value, row, index) {
                    if(value == 1){
                        var html = '<span class="label label-success">小时</span>';
                    }else if(value == 2){
                        var html = '<span class="label label-info">天</span>';//6
                    }
                    return html;
                },
                setType2:function (value, row, index) {
                    if(value == 1){
                        var html = '<span class="label label-success">在线支付</span>';
                    }else if(value == 2){
                        var html = '<span class="label label-info">余额支付</span>';//6
                    }
                    return html;
                },
                setNums:function (value, row, index) {
                    if(row.type == 1){
                        var html = value+'小时 x '+row.buy_nums;
                    }else if(row.type == 2){
                        var html = value+'天 x '+row.buy_nums;
                    }
                    return html;
                },
                setName:function (value, row, index) {

                    return row.user.id+'-'+row.user.username;
                },
                setStatus:function (value, row, index) {
                    var statusColor = row.status_color;

                    var html = '<span class="label '+statusColor+'">'+row.status_str+'</span>';
                    return html;
                },
            }
        }
    };
    return Controller;
});
